001    package EVolve;
002    
003    /* EVolve - an Extensible Software Visualization Framework
004     * Copyright (C) 2001-2002 Qin Wang
005     *
006     * This library is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Library General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This library is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Library General Public License for more details.
015     *
016     * You should have received a copy of the GNU Library General Public
017     * License along with this library; if not, write to the
018     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
019     * Boston, MA 02111-1307, USA.
020     */
021    
022    /*
023     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
024     */
025    
026    import EVolve.*;
027    import EVolve.exceptions.EVolveException;
028    import EVolve.exceptions.DataProcessingException;
029    //import EVolve.util.NumericStringComparator;
030    //import EVolve.util.InstructionAnalyzeRunner;
031    import EVolve.data.*;
032    import java.io.*;
033    import java.util.*;
034    import javax.swing.*;
035    
036    public class PMCPentiumSource implements DataSource {
037    
038        private final String dsourceName = "PMCPentiumSource";
039         private String fn = null; // data file name
040        private long LastBytecode;
041        private long eventCounter;
042    
043    
044        private int PMC_COUNTER_NUM = 2; // For Pentinum, Change to 4 for Athlon
045        private static int TAIL_LEN = 45; // The length of the last line of the trace file, where the amount of the events is recorded
046        private RandomAccessFile file;
047    
048        private EntityBuilder EVolveEventTypeBuilder;       // Give the type of an event for Evolve: GC or Ordinary  interval
049    
050        private EntityBuilder hardwareEventNameBuilder;       // Give the names of the events
051        private EntityBuilder hardwareEventDescBuilder;       // Give the descriptions of the events
052        private EntityBuilder hardwareEventIDBuilder;         // Give the IDs of the events
053        private EntityBuilder byteCodeIntervalBuilder;        // Give the default interval
054    
055        private EventBuilder hardwareEvent_1;
056        private EventBuilder hardwareEvent_2;
057        private EventBuilder cycleEvent;
058    
059        private FieldDefinition[] EVolveEventType = new FieldDefinition[PMC_COUNTER_NUM+1];                // EVolve Type, GC or Interval
060        private FieldDefinition[] intervalBytecodeNumStart= new FieldDefinition[PMC_COUNTER_NUM+1];      // Start point
061        private FieldDefinition[] intervalBytecodeNumEnd= new FieldDefinition[PMC_COUNTER_NUM+1];        // End point
062        private FieldDefinition[] intervalEventsVal= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
063    
064    
065        /*
066        private FieldDefinition[] intervalEventsDes = new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
067        private FieldDefinition[] intervalEventsName= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
068        private FieldDefinition[] intervalEventsID= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
069        */
070        private EventBuilder semiGCBuilder;
071        private FieldDefinition GCBytecodeNum;
072        private FieldDefinition[] GCEventsID= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
073        private FieldDefinition[] GCEventsName= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
074        private FieldDefinition[] GCEventsDes = new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles
075        private FieldDefinition[] GCEventsAllGCVal= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles for all GC phases
076        private FieldDefinition[] GCEventsRootScanGCVal= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles for GC phase 1
077        private FieldDefinition[] GCEventsCopyGCVal= new FieldDefinition[PMC_COUNTER_NUM+1];  // Events plus Cycles for GC phase 2
078    
079        private int[] countedEvents = new int[PMC_COUNTER_NUM+1];
080    
081    
082    
083    
084        private ElementDefinition[] definition;
085        private int definitionCounter;
086    
087        private TreeMap EVTypeMap;
088        private TreeMap IDMap;
089        private TreeMap nameMap;
090        private TreeMap descMap;
091        //private TreeMap defIntMap;
092        private TreeMap intervalMap;
093        private TreeMap GCMap;
094        private ArrayList TotalList;
095        private Iterator  ItTotalList;
096    
097    
098    
099        private String[] eventName;
100        private String[] eventDescription;
101        private String[] EVovleEventType;
102    
103        private int totalEventNum, interEventNum,gcEventNum;
104    
105    //    private InstructionAnalyzeRunner runner;
106    
107        public void init() throws EVolveException {
108            String lastName = fn;
109            LastBytecode = 0;
110            eventCounter = 0;
111            fn = Scene.getDataFileName();
112            //runner = Scene.getToolsManager().getInstructionAnalyzeRunner();
113            if (fn == null) {
114                JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastDataDir());
115                if (fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
116                    try {
117                        file = new RandomAccessFile(fc.getSelectedFile(), "r");
118                        fn = fc.getSelectedFile().getName();
119                        Scene.setDataFilename(fc.getSelectedFile().getPath());
120                        Scene.getUIManager().setLastDataDir(fc.getSelectedFile().getPath());
121                        Scene.setDataFilename(null);
122                    } catch (IOException e) {
123                        throw new EVolveException("File loading failed.");
124                    }
125                } else {
126                    fn = lastName;
127                    throw new EVolveException("File loading canceled.");
128                }
129            } else {
130                try {
131                    file = new RandomAccessFile(fn, "r");
132                } catch (IOException e) {
133                    throw new EVolveException("File loading failed.");
134                }
135            }
136    
137            // Get the length of the file
138            readLength();
139    
140            initEventNameDesc();
141        }
142    
143        public String getFileName() {
144            return fn;
145        }
146    
147        public String getName() {
148            return dsourceName;
149        }
150    
151        // Read the length of the file to get the event number
152        private void readLength()throws EVolveException {
153            // Skip to the end of the file
154    
155            try {
156                long lenOfFile = file.length();
157                file.seek(lenOfFile-TAIL_LEN);
158                String eventNumStr = file.readLine();
159                if (eventNumStr.length() < TAIL_LEN-2 ) {
160                    eventNumStr = file.readLine();
161                }
162                // Read the number of events
163                int start = eventNumStr.indexOf(' ')+1;
164                eventNumStr = eventNumStr.substring(start);
165                //start= eventNumStr.indexOf(' ') ;
166                //String InterStr= eventNumStr.substring(0,start);
167                String InterStr= eventNumStr.substring(0,20);
168                InterStr = InterStr.substring(2);
169                InterStr = InterStr.trim();
170                interEventNum = Integer.valueOf(InterStr).intValue();
171                eventNumStr = eventNumStr.substring(23);
172                eventNumStr= eventNumStr.trim();
173                gcEventNum = Integer.valueOf(eventNumStr).intValue();
174                totalEventNum = interEventNum+gcEventNum;
175                //System.out.println("total="+totalEventNum);
176            } catch (IOException e) {
177                 throw new EVolveException("FILE CHECKING FAILED");
178            }
179        }
180    
181        // Initlize the Event names and description, The XX item of these array is for the No XX event in PMc
182        // Event 257 is saved for the cycles
183        private void initEventNameDesc(){
184            eventName = new String[258];
185            eventDescription = new String[258];
186    
187            // Set the value
188    
189            eventName[40] = "L2_IFETCH";
190            eventDescription[40] = "L2 cache instruction fetches";
191    
192            eventName[41] = "L2_LD";
193            eventDescription[41] = "L2 cache data load";
194    
195            eventName[41] = "L2_ST";
196            eventDescription[41] = "L2 cache data store";
197    
198            eventName[69] = "DCU_LINE_IN";
199            eventDescription[69] = "L1 Data Cache Miss";
200    
201            eventName[72] = "DCU_MISS_OUTSTANDING";
202            eventDescription[72] = "Weighted num. of cycles whiel DCU miss is outstanding";
203    
204            eventName[121] = "CPU_CLK_UNHALTED";
205            eventDescription[121] = "Num. of cycles during the processor us not halted";
206    
207            eventName[192] = "INSTRUCTION_RETIRED";
208            eventDescription[192] = "Instructions have been executed to the end";
209    
210            eventName[257] = "CYCLE";
211            eventDescription[257] = "Executed Cycles";
212    
213    
214            EVovleEventType = new String[2];
215            EVovleEventType[0] = "Interval";
216            EVovleEventType[1] = "GC_Cycle";
217        }
218    
219    
220        public void startBuildDefinition() throws DataProcessingException {
221            String[] propertySum = {"time","sum"};
222            String[] propertyCount = {"time","count","Dotplot time"};
223            String[] propertyAmount = {"amount"};
224            String[] propertyCoordinate = {"coordinate","time"};
225            String[] propertyIndicator = {"indicator"};
226            String[] propertyBytecode = {"bytecode" };
227            String[] propertyEventValue = {"eventvalue"};
228            int i;
229            int Id1, Id2;
230    
231            definition = new ElementDefinition[5];
232    
233            EVolveEventTypeBuilder = new EntityBuilder("EventType", "Type of the Event for EVovle");
234            definition[0] = EVolveEventTypeBuilder.buildDefinition();
235    
236            byteCodeIntervalBuilder = new EntityBuilder("DefaultInterval", "The default bytecode Interval");
237            definition[1] = byteCodeIntervalBuilder.buildDefinition();
238    
239             try {
240                 file.seek(0);
241    
242                 String line;
243                // Skip the beginning text
244                for (i=0; i<5;i++ ) {
245                    file.readLine();
246                }
247                 line = file.readLine();
248                 line = line.trim();
249                 Id1 = Integer.valueOf(line).intValue();
250                 line = file.readLine();
251                 line = line.trim();
252                 Id2 = Integer.valueOf(line).intValue();
253    
254            } catch (IOException e) {
255                throw new DataProcessingException("File processing failed.");
256            }
257    
258    
259            // Build Interval Event for event 1
260            hardwareEvent_1 = new EventBuilder(eventName[Id1], eventDescription[Id1]);
261            EVolveEventType[0] = hardwareEvent_1.buildReferenceDefinition("Type",EVolveEventTypeBuilder,null,"Type of this record");
262            intervalBytecodeNumStart[0] = hardwareEvent_1.buildValueDefinition("Bytecode Start", propertyBytecode, "Bytecode sequence startpoint");
263            intervalBytecodeNumEnd[0] = hardwareEvent_1.buildValueDefinition("Bytecode End", propertyBytecode, "Bytecode sequence endpoint");
264            intervalEventsVal[0] = hardwareEvent_1.buildValueDefinition("Number of Event "+eventName[Id1], propertyEventValue, "Occurence Number of Event"+eventName[Id1]);
265            definition[2] = hardwareEvent_1.buildDefinition();
266    
267            // Build Interval Event for event 2
268            hardwareEvent_2 = new EventBuilder(eventName[Id2], eventDescription[Id2]);
269            EVolveEventType[1] = hardwareEvent_2.buildReferenceDefinition("Type",EVolveEventTypeBuilder,null,"Type of this record");
270            intervalBytecodeNumStart[1] = hardwareEvent_2.buildValueDefinition("Bytecode Start", propertyBytecode, "Bytecode sequence startpoint");
271            intervalBytecodeNumEnd[1] = hardwareEvent_2.buildValueDefinition("Bytecode End", propertyBytecode, "Bytecode sequence endpoint");
272            intervalEventsVal[1] = hardwareEvent_2.buildValueDefinition("Number of Event"+eventName[Id2], propertyEventValue, "Occurence Number of Event"+eventName[Id2]);
273            definition[3] = hardwareEvent_2.buildDefinition();
274    
275            // Build Interval Event for cycle
276            cycleEvent = new EventBuilder(eventName[257], eventDescription[257]);
277            EVolveEventType[2] = cycleEvent.buildReferenceDefinition("Type",EVolveEventTypeBuilder,null,"Type of this record");
278            intervalBytecodeNumStart[2] = cycleEvent.buildValueDefinition("Bytecode Start", propertyBytecode, "Bytecode sequence startpoint");
279            intervalBytecodeNumEnd[2] = cycleEvent.buildValueDefinition("Bytecode End", propertyBytecode, "Bytecode sequence endpoint");
280            intervalEventsVal[2] = cycleEvent.buildValueDefinition("Number of Event"+eventName[257], propertyEventValue, "Occurence Number of Event"+eventName[257]);
281            definition[4] = cycleEvent.buildDefinition();
282            definitionCounter = -1;
283        }
284    
285        public ElementDefinition getNextDefinition() throws DataProcessingException {
286            definitionCounter++;
287            if (definitionCounter < definition.length) {
288                return definition[definitionCounter];
289            } else {
290                return null;
291            }
292        }
293    
294        private String getSub(String line, int part) {
295            int start = line.indexOf('\t') + 1;
296            int i = 1;
297            while (i < part) {
298                start = line.indexOf('\t', start) + 1;
299                i++;
300            }
301            int end = line.indexOf('\t', start);
302            if (end == -1) {
303                return line.substring(start);
304            } else {
305                return line.substring(start, end);
306            }
307        }
308    
309        public void startBuildEntity() throws DataProcessingException {
310            String line;
311            try {
312                file.seek(0);
313                // Skip the beginning text
314                for (int i=0; i<5;i++ ) {
315                    file.readLine();
316                }
317    
318                EVTypeMap = new TreeMap();
319                IDMap = new TreeMap();
320                descMap = new TreeMap();
321                nameMap = new TreeMap();
322                intervalMap = new TreeMap();
323                GCMap = new TreeMap();
324                TotalList = new ArrayList();
325    
326            } catch (IOException e) {
327                throw new DataProcessingException("File processing failed.");
328            }
329    
330            // Read the entities here
331            int  Id;
332            try {
333                    Entity newVal = null;
334                    for (int i=0; i<PMC_COUNTER_NUM;i++ ) {
335    
336                            line = file.readLine();
337                            line = line.trim();
338                            Id = Integer.valueOf(line).intValue();
339                            countedEvents[i] = Id;
340                            //hardwareEventIDBuilder.newEntity(line);
341                            //newVal = hardwareEventIDBuilder.buildEntity();
342                            //IDMap.put(line,newVal);
343                            //TotalList.add(newVal);
344                            //hardwareEventNameBuilder.newEntity(eventName[Id]);
345                            //newVal = hardwareEventNameBuilder.buildEntity();
346                            //nameMap.put(eventName[Id],newVal);
347                            //TotalList.add(newVal);
348                            //hardwareEventDescBuilder.newEntity(eventDescription[Id]);
349                            //newVal = hardwareEventDescBuilder.buildEntity();
350                            //descMap.put(eventDescription[Id],newVal);
351                            //TotalList.add(newVal);
352                    }
353                    line="257";
354                    countedEvents[PMC_COUNTER_NUM] = 257;
355                    String str = "CYCLE";
356                    String str2 = "Executed Cycles";
357                    //hardwareEventIDBuilder.newEntity(line);
358                    //newVal = hardwareEventIDBuilder.buildEntity();
359                    //IDMap.put(line,newVal);
360                    //TotalList.add(newVal);
361                    //hardwareEventNameBuilder.newEntity(str);
362                    //newVal = hardwareEventNameBuilder.buildEntity();
363                    //nameMap.put(str,newVal);
364                    //TotalList.add(newVal);
365                    //hardwareEventDescBuilder.newEntity(str2);
366                    //newVal = hardwareEventDescBuilder.buildEntity();
367                    ///descMap.put(str2,newVal);
368                    //TotalList.add(newVal);
369    
370                    EVolveEventTypeBuilder.newEntity(EVovleEventType[0]);
371                    newVal = EVolveEventTypeBuilder.buildEntity();
372                    EVTypeMap.put(EVovleEventType[0],newVal);
373                    TotalList.add(newVal);
374    
375                    EVolveEventTypeBuilder.newEntity(EVovleEventType[1]);
376                    newVal = EVolveEventTypeBuilder.buildEntity();
377                    EVTypeMap.put(EVovleEventType[1],newVal);
378                    TotalList.add(newVal);
379    
380                    // For the interval
381                    line = file.readLine();
382                    line = line.trim();
383                    byteCodeIntervalBuilder.newEntity(line);
384                    newVal = byteCodeIntervalBuilder.buildEntity();
385                    TotalList.add(newVal);
386    
387            } catch (IOException e) {
388                  throw new DataProcessingException("File processing failed.");
389            }
390    
391            ItTotalList = TotalList.iterator();
392        }
393    
394    
395        public Entity getNextEntity() throws DataProcessingException {
396            Entity returnVal = null;
397    
398            try {
399                    returnVal = (Entity)ItTotalList.next();
400                    return returnVal;
401    
402            } catch (NoSuchElementException e) {
403                    returnVal = null;
404                    return returnVal;
405            }
406        }
407    
408        public long getTotalNumberOfEvents() {
409            return totalEventNum * 3 ;
410        }
411    
412        public long getNumberOfEvents(String definitionName) {
413    
414           // if (definitionName.equalsIgnoreCase("HardwareEventCount"))
415                return totalEventNum;
416            //else
417            //    return 0;
418        }
419    
420        public void startBuildEvent() throws DataProcessingException {
421            try {
422                file.seek(0);
423                // Skip the beginning text
424                for (int i=0; i<5;i++ ) {
425                    file.readLine();
426                }
427                // Skip the first three lines for the entities
428                for (int i=0; i<PMC_COUNTER_NUM+1;i++ ) {
429                    file.readLine();
430                }
431    
432                eventCounter = 0;
433            } catch (IOException e) {
434                throw new DataProcessingException("File processing failed.");
435            }
436        }
437    
438        public Event getNextEvent() throws DataProcessingException {
439    
440            eventCounter++;
441            try {
442    
443    
444                if ((eventCounter%3) == 2) return hardwareEvent_2.buildEvent();
445                else if ((eventCounter%3) == 0) return cycleEvent.buildEvent();
446    
447                Event returnVal = null;
448                String line = file.readLine();
449    
450                /* Now Event Counter % 3 == 1 */
451                while ((returnVal == null) && (line != null)) {
452                    line = line.trim();
453                    if (line.length() < 2)
454                    {
455                       line = file.readLine();
456                       continue;
457                    }
458                    char ch = line.charAt(0);
459    
460                    if (ch == 'I') {  // This is an Interval Event
461                        hardwareEvent_1.newEvent();
462                        hardwareEvent_2.newEvent();
463                        cycleEvent.newEvent();
464    
465                        hardwareEvent_1.addReferenceField(EVolveEventType[0],(Entity)EVTypeMap.get(EVovleEventType[0])); // Inteval
466                        hardwareEvent_2.addReferenceField(EVolveEventType[1],(Entity)EVTypeMap.get(EVovleEventType[0])); // Inteval
467                        cycleEvent.addReferenceField(EVolveEventType[2],(Entity)EVTypeMap.get(EVovleEventType[0])); // Inteval
468    
469                        String bytecodeStr = getSub(line, 1);
470                        bytecodeStr = bytecodeStr.trim();
471                        int val = Integer.valueOf(bytecodeStr).intValue(); // Currently use integer, should be changed into long long int later
472                        hardwareEvent_1.addValueField(intervalBytecodeNumStart[0],LastBytecode);
473                        hardwareEvent_1.addValueField(intervalBytecodeNumEnd[0],val);
474                        hardwareEvent_2.addValueField(intervalBytecodeNumStart[1],LastBytecode);
475                        hardwareEvent_2.addValueField(intervalBytecodeNumEnd[1],val);
476                        cycleEvent.addValueField(intervalBytecodeNumStart[2],LastBytecode);
477                        cycleEvent.addValueField(intervalBytecodeNumEnd[2],val);
478    
479                        LastBytecode = val;
480    
481                        String evtValStr = getSub(line, 2);
482                        evtValStr = evtValStr.trim();
483                        long evtVal = Long.valueOf(evtValStr).longValue();
484                        hardwareEvent_1.addValueField(intervalEventsVal[0],evtVal);
485    
486                        evtValStr = getSub(line, 3);
487                        evtValStr = evtValStr.trim();
488                        evtVal = Long.valueOf(evtValStr).longValue();
489                        hardwareEvent_2.addValueField(intervalEventsVal[1],evtVal);
490    
491                        evtValStr = getSub(line, 4);
492                        evtValStr = evtValStr.trim();
493                        evtVal = Long.valueOf(evtValStr).longValue();
494                        cycleEvent.addValueField(intervalEventsVal[2],evtVal);
495                        returnVal = hardwareEvent_1.buildEvent();
496    
497                    } else if (ch == 'G') { // This is a GC Event
498    
499                        hardwareEvent_1.newEvent();
500                        hardwareEvent_2.newEvent();
501                        cycleEvent.newEvent();
502    
503                        hardwareEvent_1.addReferenceField(EVolveEventType[0],(Entity)EVTypeMap.get(EVovleEventType[1])); // GC
504                        hardwareEvent_2.addReferenceField(EVolveEventType[1],(Entity)EVTypeMap.get(EVovleEventType[1])); // GC
505                        cycleEvent.addReferenceField(EVolveEventType[2],(Entity)EVTypeMap.get(EVovleEventType[1])); // GC
506    
507                        String bytecodeStr = getSub(line, 1);
508                        bytecodeStr = bytecodeStr.trim();
509                        int val = Integer.valueOf(bytecodeStr).intValue(); // Currently use integer, should be changed into long long int later
510                        hardwareEvent_1.addValueField(intervalBytecodeNumStart[0],LastBytecode);
511                        hardwareEvent_1.addValueField(intervalBytecodeNumEnd[0],val);
512                        hardwareEvent_2.addValueField(intervalBytecodeNumStart[1],LastBytecode);
513                        hardwareEvent_2.addValueField(intervalBytecodeNumEnd[1],val);
514                        cycleEvent.addValueField(intervalBytecodeNumStart[2],LastBytecode);
515                        cycleEvent.addValueField(intervalBytecodeNumEnd[2],val);
516    
517                        LastBytecode = val;
518    
519                        String evtValStr = getSub(line, 2);
520                        evtValStr = evtValStr.trim();
521                        long evtVal = Long.valueOf(evtValStr).longValue();
522                        hardwareEvent_1.addValueField(intervalEventsVal[0],evtVal);
523    
524                        evtValStr = getSub(line, 3);
525                        evtValStr = evtValStr.trim();
526                        evtVal = Long.valueOf(evtValStr).longValue();
527                        hardwareEvent_2.addValueField(intervalEventsVal[1],evtVal);
528    
529                        evtValStr = getSub(line, 4);
530                        evtValStr = evtValStr.trim();
531                        evtVal = Long.valueOf(evtValStr).longValue();
532                        cycleEvent.addValueField(intervalEventsVal[2],evtVal);
533                        returnVal = hardwareEvent_1.buildEvent();
534    
535    
536                    }
537    
538                    if (returnVal == null) {
539                        line = file.readLine();
540                    }
541                }
542                return returnVal;
543            } catch (IOException e) {
544                throw new DataProcessingException("File processing failed.");
545            }
546        }
547    
548    }